package endereco;

import java.util.List;

import persistencia.DAOException;
import persistencia.DAOFactory;
import persistencia.RNException;

public class EnderecoRN {

	private EnderecoDAO enderecoDAO = DAOFactory.getEnderecoDAO(); 
	private EnderecoDAOHibernate enderecoDAOHibernate = new EnderecoDAOHibernate();
	
	public void salvar(Endereco endereco) throws RNException {
		try {
			enderecoDAO.getSession().beginTransaction();
			enderecoDAO.save(endereco);
			enderecoDAO.getSession().getTransaction().commit();
		} catch (DAOException e) {
			e.printStackTrace();
			throw new RNException();
		}
	}

	public List<Endereco> listar() throws RNException {

		try {
			return enderecoDAO.findAll();

		} catch (DAOException e) {
			e.printStackTrace();
			throw new RNException();
		}
	}

	public Endereco buscaPorCep(String cep) {
		return enderecoDAOHibernate.buscaPorCep(cep);
	}
	
	public void deletar(Endereco endereco)throws RNException{
		try {
			enderecoDAO.getSession().beginTransaction();
			enderecoDAO.delete(endereco);
			enderecoDAO.getSession().getTransaction().commit(); 
			
		} catch (DAOException e) {
			e.printStackTrace();
			throw new RNException(); 
		}
	}


}
